
<?php
	require_once ('C:\web\php\pear\Mail.php');
	require('C:\web\php\Smarty\Smarty.class.php');

	date_default_timezone_set ('Europe/Warsaw');

	$ErrorArray = array();
	$ErrRecipentRequired = "Recipient is required";
	$ErrEmailRequired = "Email is required";
	$ErrEmailInvalid = "Invalid email format";
	$ErrSubjectRequired = "Subject is required";
	$ErrContentRequired = "Content is required";
	$TemplateEmailForm = "contact2.tpl";
	$TemplateEmailConfirm = "confirm.tpl";
	
	
	$Subject = $Email = $SendTo = $Content = "";

	$IniArray = parse_ini_file("lib.ini",true);
	
	$smarty = new Smarty();

	$smarty->setCompileDir('C:\web\php\smarty\templates_c');
	$smarty->setCacheDir('C:\web\php\smarty\cache');
	$smarty->setConfigDir('C:\web\php\smarty\configs');
	
	$display_template = $TemplateEmailForm;


	if ($_SERVER["REQUEST_METHOD"] == "POST") 
	{
		if (empty($_POST["SendTo"])) 
		{
			$ErrorArray[] = $ErrRecipentRequired;
			$smarty->assign('ErrRecipent', $ErrRecipentRequired);
		} 
		else
		{
			$SendTo = test_input($_POST["SendTo"]);
		}
		
		if (empty($_POST["Email"]))
		{
			$ErrorArray[] = $ErrEmailRequired;
			$smarty->assign('ErrEmail', $ErrEmailRequired);
		} 
		else 
		{
			$Email = test_input($_POST["Email"]);
			if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$Email)) 
			{
				$ErrorArray[] = $ErrEmailInvalid; 
				$smarty->assign('ErrEmail', $ErrEmailInvalid);
			}
		}
  
		if (empty($_POST["Subject"]))  
		{
			$ErrorArray[] = $ErrSubjectRequired;
			$smarty->assign('ErrSubject', $ErrSubjectRequired);
		} 
		else
		{
			$Subject = test_input($_POST["Subject"]);
		}
  
		if (empty($_POST["Content"]))
		{
			$ErrorArray[] = $ErrContentRequired;
			$smarty->assign('ErrContent', $ErrContentRequired);
		} 
		else 
		{
			$Content = test_input($_POST["Content"]);
		}	
		
		if (empty($ErrorArray))
		{
			send_mail($IniArray['Email'][$SendTo],$Email,$Subject,$Content);
		}
		
	}
	
	$smarty->assign('Email', $Email);
	$smarty->assign('Content', $Content);
	$smarty->assign('Subject', $Subject);
	$smarty->assign('SendTo', $SendTo);
	
	$smarty->display( $display_template );
	
	
	
	function send_mail ($to, $from, $Subject, $body)
	{	
		/*
		$from = '<jurek1029@gmail.com>';
		$to = '<jurek1029@gmail.com>';
		$Subject = 'Hi!';
		$body = "Hi,\n\nHow are you?";
		*/

		$headers = array(
		'From' => $from,
		'To' => $to,
		'Subject' => this.$Subject
		);

		$smtp = Mail::factory('smtp', array(
			'host' => 'ssl://smtp.gmail.com',
			'port' => '465',
			'auth' => true,
			'username' => 'bscmailtest@gmail.com',
			'password' => 'niktniezgadnie'
		));
	
		$mail = $smtp->send($to, $headers, $body);

		if (PEAR::isError($mail)) 
		{
			echo('<p>' . $mail->getMessage() . '</p>');
		} 
		else 
		{
			$display_template = $TemplateEmailConfirm;
		}
	}
	
	function test_input($data) {
	   $data = trim($data);
	   $data = stripslashes($data);
	   $data = htmlspecialchars($data);
	   return $data;
	}

	
	echo "<br>";
	for($x =0;$x<count($ErrorArray);$x++)
	{
		echo $ErrorArray[$x] ;
		echo "<br>";
	} 
	
?>
